<template>
    <div class="entity-to-json">
        <div class="background"></div>

        <div class="breadcrumb">
            <el-breadcrumb separatorClass="el-icon-arrow-right">
                <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
                <el-breadcrumb-item>实体类转JSON</el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div class="buttons">
            <el-button @click="toClick">转换</el-button>
        </div>
        <div class="content">
            <el-input v-model="entity" class="textarea" type="textarea" :rows="30"></el-input>
            <el-input v-model="json" class="textarea" type="textarea" :rows="30"></el-input>
        </div>

    </div>
</template>

<script type="text/ecmascript-6">
    /**
     * @author 白雨浓
     * @date 2018/5/14 21:10
     *
     * 实体类转JSON
     **/
    export default {
        name: 'AcEntityToJSON',
        data() {
            return {
                entity: '',
                json: '',

            }
        },
        mounted() {
            this.$nextTick(() => {

            })
        },
        methods: {
            toClick() {
                const list = this.entity.split('\n');

                const params = list.filter(p => p.includes('private'));
                for (let i = 0; i < params.length; i++) {
                    params[i] = params[i].trim().replace(/\t/g, '')
                        .replace(';', '')
                        .split(' ')[2];
                }

                const annos = [];
                list.forEach((item, index) => {
                    if (!item.includes('private')) return;

                    const annoReg = new RegExp('/\\*\\*([\\s\\S]+?)\\*/', 'gm');

                    for (let i = index; i > index - 3; i--) {
                        let anno = list[i].trim();
                        let res;
                        if (anno.includes('*')
                            && !anno.includes('/**')
                            && !anno.includes('*/')) {
                            annos.push(anno.replace('@mbg.generated ', '')
                                .replace(/\t/g, '')
                                .replace(/ /g, '')
                                .replace(/\*/g, ''));
                            break;
                        } else if (res = annoReg.exec(anno)) {
                            annos.push(res[1].trim());
                        }
                    }
                });

                if (annos.length !== 0 && params.length !== annos.length) {
                    this.$$msg.error('请补全注释');
                }

                let json = '';
                for (let i = 0; i < params.length; i++) {
                    if (params[i])
                        json += `${params[i]}: null,`;
                    if (annos[i])
                        json += `/*${annos[i]}*/`;
                    json += '\n';
                }
                this.json = json;
            }
        }
    }
</script>

<style lang="stylus" rel="stylesheet/stylus" scoped>
    .entity-to-json {
    }

    .background {
        position fixed
        top 0
        left 0
        z-index -999
        width 100%
        height 100%
        background-image url("../../assets/index/background.png")
        background-size cover
    }

    .breadcrumb {
        display flex
        justify-content space-between
        align-items center
        height 30px
        padding 20px
        background-color rgba(255, 255, 255, .3)
    }

    .buttons {
        width 80%
        margin 10px auto
        text-align right
    }

    .content {
        display flex
        justify-content space-between
        width 80%
        margin auto
    }

    .textarea {
        width 50%
    }
</style>
